package com.lzwk.app.function;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.apache.flink.table.functions.ScalarFunction;

import java.math.BigDecimal;

/**
 * @title: SkinJson
 * @Author Yeaser
 * @Date: 2022/1/5 17:51
 * @Version 1.0
 * Description: TODO
 */
// @FunctionHint(output = @DataTypeHint("row<source_hash_id string>")) 表值函数
public class GetJsonDouble extends ScalarFunction {
    public Double eval(String json, String key,Double defaultData){

        if(json == null) {

            return defaultData;

        }else if (json.contains(key)){//这里不能先解析json再用containsKey方法去判断，部分不为null的数据传进来会报空指针，但具体是哪些数据暂时捕捉不到

            JSONObject jsonObject = JSON.parseObject(json);

            return Double.valueOf(jsonObject.getBigDecimal(key).toString());

        }else{

            return defaultData;

        }

    }

    public static void main(String[] args) {
        String s = JSON.toJSONString("{target_roi:0.00}");
        JSONObject jsonObject = JSON.parseObject("{target_roi:1.00}");
        BigDecimal target_roi = jsonObject.getBigDecimal("target_roi");
        Double aDouble = Double.valueOf(target_roi.toString());
        System.out.println(aDouble);
    }
}
